<?php 
class boutique_envoyer extends MX_Controller{
	public function mail($id=''){
		
		if($this->input->post('submit')) return self::envoyer_annonce();

		$boutiqueInfo= $this->load->Module('boutique/show');
		$boutique=$boutiqueInfo->get_info($id);
		
		
		$keywords=$description=$title=$boutique='';
		if(!is_numeric($id))
			$id=$this->input->post('id');
		if(is_numeric($id)){
			$boutique=$boutiqueInfo->get_info($id);		
			$title="Envoyer message à ".$boutique->company_name;
			$description="Vous êtes intéressé par les annonces de ".$boutique->company_name;
		}
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'boutique'=>$boutique,				
				'filter'=>''
		);

		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		
		
		$this->view(
				array('header',
					'boutique-envoyer-mail',
					'footer'), 
				$data); // Display the page
	}
	public function envoyer_annonce(){

		$id=$this->input->post('id');
		
		$setting=$this->load->Module('setting/data');
		
		$boutiqueInfo= $this->load->Module('boutique/show');
		
		if(is_numeric($id)) $boutique=$boutiqueInfo->get_info($id);
		
		if(!$boutique){
			$data=array(
					'title'=>"Ce boutique est désactivée",
					'description'=>"boutique non-trouvable",
					'keywords'=>''
			);
				
			$data=$setting->data($data);
			$this->view(
					array('header',
							'annonce-desactive',
							'footer'),
							$data); // Display the page
			return false;
		}
				
		$option=$this->load->module('setting/option');
		
		//send email
		$email= $this->load->Module('send/email');
		//$settings=$this->load->Module('setting/admin');
		$edition=$this->load->Module('setting/edition');
		//$setting_admin=$settings->get_admin();
		$edition_send=$edition->get_Edition('contact_boutique');
		
		$from_email=$this->input->post('email');
		$from_name=$this->input->post('name');
		$to_email=$boutique->email;
		$subject=$this->input->post('subject');
		$message=$edition_send[0]->setting;
		$message_data=(array)$boutique;
		$message_data['from_email']=$from_email;
		$message_data['from_name']=$from_name;
		$message_data['phone']=$this->input->post('telephone');
		
		$message_data['phishing']=$this->config->config['base_url'].'phishing.html';
		
		foreach ($message_data as $key=>$data){
			if(is_string ($data)) $message=str_replace('{'.$key.'}',$data,$message);
		}
		
		$message=strip_tags($this->input->post('message')).$message;
		
		$status='send';
		$action='send_to_shop';
		$description='';
		
		$email->add($from_email,$from_name,$to_email,$subject,$message,$status,$action,$description);
		
		$data=array(
				'title'=>"Vous avez envoyé un message à ".$boutique->company_name,
				'description'=>"Vous aimez les annonces de ".$boutique->company_name,
				'keywords'=>"Conseiller annonce ami, envoyer annonce à",
				'filter'=>'',
				'boutique'=>$boutique
		);
		
		$data=$setting->data($data);
		$this->view(
				array('header',
						'boutique-envoyer-message',
						'footer'),
				$data); // Display the page
			}
		

}